package com.jinzhi.jinzhidata.controller;

import com.jinzhi.jinzhidata.vo.ArticleDetailVO;
import com.jinzhi.jinzhidata.vo.ListArticleVO;
import com.jinzhi.jinzhidata.vo.ResultVO;
import com.jinzhi.jinzhidata.dataobject.Article;
import com.jinzhi.jinzhidata.dataobject.ArticleContent;
import com.jinzhi.jinzhidata.dataobject.User;
import com.jinzhi.jinzhidata.enums.ResultEnum;
import com.jinzhi.jinzhidata.service.ArticleContentService;
import com.jinzhi.jinzhidata.service.ArticleService;
import com.jinzhi.jinzhidata.service.UserService;
import com.jinzhi.jinzhidata.util.ResultVOUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private ArticleContentService articleContentService;

    @Autowired
    private UserService userService;

    @GetMapping("/count")
    public ResultVO articleCount() {
        Long count = articleService.countPublish();
        return ResultVOUtil.success(count);
    }

    /**
     * 文章列表
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list")
    public ResultVO articleList(@RequestParam Integer page, @RequestParam Integer size) {
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        PageRequest pageRequest = PageRequest.of(page, size, sort);
        List<Article> articleList = articleService.selectArticleListPublish(pageRequest);
        List<ListArticleVO> listArticleVOList = new ArrayList<>();
        articleList.forEach(article -> {
            ListArticleVO listArticleVO = new ListArticleVO();
            BeanUtils.copyProperties(article, listArticleVO);
            listArticleVOList.add(listArticleVO);
        });
        return ResultVOUtil.success(listArticleVOList);
    }


    /**
     * 获取文章内容
     *
     * @param articleId
     * @return
     */
    @GetMapping(value = "/{articleId}")
    public ResultVO showArticle(@PathVariable String articleId) {
        if (StringUtils.isEmpty(articleId)) {
            return ResultVOUtil.error(ResultEnum.NULL_ERROR);
        }
        Article article = articleService.selectByArticleId(articleId);
        if (article == null) {
            return ResultVOUtil.error(ResultEnum.NULL_ERROR);
        }
        ArticleDetailVO articleDetailVO = new ArticleDetailVO();
        BeanUtils.copyProperties(article, articleDetailVO);
        ArticleContent articleContent = articleContentService.selectByArticleContentId(article.getArticleContentId());
        articleDetailVO.setArticleContent(articleContent.getArticleContentText());
        User author = userService.selectUserByUserId(article.getArticleAuthorId());
        articleDetailVO.setArticleAuthorName(author.getUserName());
        return ResultVOUtil.success(articleDetailVO);
    }

}
